<div class="content-wrapper">
    <?php
    $form = $this->form;
    $form->prepare();

    $form->get('adminGroupName')->setAttributes([
        'placeholder' => $this->translate('管理员组名称')
    ]);

    ?>
    <?php echo $this->form()->openTag($form); ?>
    <section class="content-header">
        <?php echo $this->partial('layout/breadcrumb'); ?>

        <h1 class="pull-right" id="dberp-menu-top">
            <a href="<?php echo $this->url('admin-group', ['action'=> 'adminGroupList']);?>" class="btn btn-default btn-sm"><i class="fa fa-arrow-left"></i> <?php echo $this->translate('返回管理员组列表'); ?></a>
            <button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-check"></i> <?php echo $this->translate('保存管理员组'); ?></button>
            <?php echo (isset($group) ? $this->HelpUrl('adminGroupEdit') : $this->HelpUrl('adminGroupAdd')); ?>
        </h1>
    </section>

    <section class="content">

        <div class="row">
            <div class="col-md-12">
                <div class="nav-tabs-custom">
                    <ul class="nav nav-tabs">
                        <li class="active"><a href="#tab_1" data-toggle="tab"><?php echo $this->translate('基本设置'); ?></a></li>
                        <?php if(!isset($this->group) || $this->group->getAdminGroupId() != 1) { ?>
                            <li><a href="#tab_2" data-toggle="tab"><?php echo $this->translate('权限设置'); ?></a></li>
                            <li><a href="#tab_3" data-toggle="tab"><?php echo $this->translate('菜单设置'); ?></a></li>
                        <?php } ?>
                    </ul>
                    <div class="tab-content">
                        <?php echo $this->partial('layout/messages'); ?>
                        <div class="tab-pane active" id="tab_1">
                            <div class="box-body">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('管理员组'); ?><span class="must_add_value">*</span></label>
                                    <div class="col-sm-6">
                                        <?php echo $this->formElement($form->get('adminGroupName')); ?>
                                        <?php echo $this->formElementErrors($form->get('adminGroupName'), ['class'=>'error-message']); ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <?php if(!isset($this->group) || $this->group->getAdminGroupId() != 1) { ?>
                        <div class="tab-pane" id="tab_2">
                            <div class="box-body">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('权限设置'); ?></label>
                                    <div class="col-sm-6">
                                        <label class="checkbox-inline">
                                            <input type="checkbox" name="allSelect" id="allSelect" value="1" class="minimal"><?php echo $this->translate('全选'); ?>
                                        </label>
                                    </div>
                                </div>

                                <?php
                                if($this->group && !empty($this->group->getAdminGroupPurview())) $groupPermission = explode(',', $this->group->getAdminGroupPurview());
                                foreach ($this->permissionArray as $key => $permission) { ?>
                                    <div class="form-group">
                                        <label class="col-sm-2 control-label"><?php echo $this->translate($permission['name']); ?></label>
                                        <div class="col-sm-6">
                                            <label class="checkbox-inline">
                                                <input type="checkbox" value="1" id="<?php echo $key?>" class="minimal erp-group"><?php echo $this->translate($permission['name']); ?><?php echo $this->translate('全选'); ?>
                                            </label>
                                        </div>
                                    </div>
                                    <?php foreach ($permission['controllers'] as $controllerKey => $controllerValue) {
                                        $controllerStr = str_replace('\\', '_', $controllerKey);
                                        ?>
                                        <div class="form-group">
                                            <div class="col-sm-2 control-label"><?php echo $this->translate($controllerValue['name']); ?> <input type="checkbox" value="1" class="minimal <?php echo $key?> erp-group-one" id="<?php echo $controllerStr?>"></div>
                                            <div class="col-sm-10">
                                                <?php foreach ($controllerValue['action'] as $value) {
                                                    $pName = $controllerStr . '_' . $value;
                                                    ?>
                                                    <label class="checkbox-inline">
                                                        <input type="checkbox" name="adminGroupPermission[]" class="minimal <?php echo $key?> <?php echo $controllerStr?>" <?php echo (isset($groupPermission) && in_array($pName, $groupPermission)) ? 'checked' : ''?> value="<?php echo $pName?>"><?php echo $this->translate($controllerValue['actionNames'][$value]); ?>
                                                    </label>
                                                <?php } ?>
                                            </div>
                                        </div>
                                    <?php } ?>
                                <?php } ?>
                            </div>
                        </div>
                        <div class="tab-pane" id="tab_3">
                            <div class="box-body">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('菜单设置'); ?></label>
                                    <div class="col-sm-6">
                                        <label class="checkbox-inline">
                                            <?php echo $this->formElement($form->get('adminMenuState')); ?> <?php echo $this->translate('启用'); ?>
                                            <?php echo $this->formElementErrors($form->get('adminMenuState'), ['class'=>'error-message']); ?>
                                        </label>
                                    </div>
                                </div>
                                <hr>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('菜单'); ?></label>
                                    <div class="col-sm-6">
                                        <label class="checkbox-inline">
                                            <input type="checkbox" name="allMenuSelect" id="allMenuSelect" value="1" class="minimal"><?php echo $this->translate('全选'); ?>
                                        </label>
                                    </div>
                                </div>
                                <?php
                                if($this->group && !empty($this->group->getAdminMenuBody())) $groupMenu = explode(',', $this->group->getAdminMenuBody());
                                foreach ($this->menuArray as $key => $menuValue) { ?>
                                    <div class="form-group">
                                        <label class="col-sm-2 control-label"><?php echo $this->translate($menuValue['name']); ?> <input type="checkbox" name="groupMenu[]" <?php echo (isset($groupMenu) && in_array($key, $groupMenu)) ? 'checked' : ''?> value="<?php echo $key; ?>" class="minimal <?php echo $key?> erp-group-menu" id="<?php echo $key; ?>-erp-group-menu"></label>
                                        <div class="col-sm-10">
                                            <?php foreach ($menuValue['controllers'] as $controllerKey => $controllerValue) {
                                                $controllerStr = str_replace('\\Controller\\', '', $controllerKey);
                                                ?>
                                                <label class="checkbox-inline">
                                                    <input type="checkbox" name="groupMenu[]" class="minimal erp-group-menu-one" data-value="<?php echo $key; ?>-erp-group-menu" <?php echo (isset($groupMenu) && in_array($controllerStr, $groupMenu)) ? 'checked' : ''?> value="<?php echo $controllerStr; ?>"><?php echo $this->translate($controllerValue['name']); ?>
                                                </label>
                                            <?php } ?>
                                        </div>
                                    </div>
                                <?php } ?>
                            </div>
                        </div>
                        <?php } ?>
                    </div>
                </div>
            </div>
        </div>

    </section>
<?php echo $this->form()->closeTag(); ?>

</div>
<script type="text/javascript">
    $(document).ready(function() {
        $("#<?php echo $form->getName(); ?>").validate({
            rules: {
                adminGroupName: {
                    required: true
                }
            },
            messages: {
                adminGroupName: {
                    required: "<?php echo $this->translate('管理员组不能为空！'); ?>"
                }
            }
        });
    });

    $("#allSelect").click(function () {
        if(!this.checked) {
            $("input[name='adminGroupPermission[]']").prop("checked", false);
            $(".erp-group,.erp-group-one").prop("checked", false);
        } else {
            $("input[name='adminGroupPermission[]']").prop("checked", true);
            $(".erp-group,.erp-group-one").prop("checked", true);
        }
    });

    $(".erp-group").click(function () {
        if(!this.checked) {
            $("."+this.id).prop("checked", false);
        } else {
            $("."+this.id).prop("checked", true);
        }
    });

    $(".erp-group-one").click(function () {
        if(!this.checked) {
            $("."+this.id).prop("checked", false);
        } else {
            $("."+this.id).prop("checked", true);
        }
    });

    $("#allMenuSelect").click(function () {
        if(!this.checked) {
            $("input[name='groupMenu[]']").prop("checked", false);
        } else {
            $("input[name='groupMenu[]']").prop("checked", true);
        }
    });
    $(".erp-group-menu-one").click(function (){
        let value = $(this).attr('data-value');
        if(!this.checked) {
            $("#"+value).prop("checked", false);
        } else {
            $("#"+value).prop("checked", true);
        }
    });
</script>
